package com.hns.frame.mybatis;

import java.io.Serializable;
import java.util.List;

import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
/**
 * 翻页对象
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class Pagination implements Serializable {
    private static final long serialVersionUID = -2941676918809439314L;
    
    private int          total = 0;
    private List<Object> data;
    private RowBoundsExt    rowBounds;
    
    public Pagination(RowBoundsExt rowbound, int total) {
        this.rowBounds = new RowBoundsExt(rowbound.getPageNumber(), rowbound.getLimit());
        this.total = total;
    }
    
    public Pagination(RowBoundsExt rowBounds, int total, List<Object> data) {
        this.rowBounds = new RowBoundsExt(rowBounds.getPageNumber(), rowBounds.getLimit());
        this.total = total;
        setData(data);
    }
    
    public RowBoundsExt getRowBounds() {
        return new RowBoundsExt(this.rowBounds.getPageNumber(), this.rowBounds.getLimit());
    }
    
    public List<Object> getData() {
        return this.data;
    }
    
    public void setData(List<Object> data) {
        this.data = data;
        if ((data != null) && (data.size() > this.total))//此情况出现在数据总数小于页长，并且频繁插入数据
//            throw new BusinessException(MsgType.ERROR.getFlag(),"data.size("+data.size()+")>total("+this.total+").please check total.");
        	this.total = data.size();
    }
    
    public int getTotal() {
        return this.total;
    }
    
    public int getPageTotal() {
        int size = this.rowBounds.getLimit();
        return (int) Math.ceil(this.total * 1.0D / size);
    }
}
